using System;
using System.Drawing;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;


namespace EpisodeWizard.App
{
	/// <summary>
	/// Summary description for FileProperties.
	/// </summary>
	public class FileGuesses : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textSelectedShow;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListView listShows;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ListView listEpisodes;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ListView listTitles;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button btnSelectShow;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnSelectEpisode;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textSelectedSeason;
		private System.Windows.Forms.Button btnSelectTitle;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textSelectedTitle;
		private TextBox textSelectedEpisode;
		private Label label7;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		public FileGuesses()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FileGuesses));
			this.textSelectedShow = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.listShows = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.label2 = new System.Windows.Forms.Label();
			this.listEpisodes = new System.Windows.Forms.ListView();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.label3 = new System.Windows.Forms.Label();
			this.listTitles = new System.Windows.Forms.ListView();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.label4 = new System.Windows.Forms.Label();
			this.btnSelectShow = new System.Windows.Forms.Button();
			this.btnSelectEpisode = new System.Windows.Forms.Button();
			this.label5 = new System.Windows.Forms.Label();
			this.textSelectedSeason = new System.Windows.Forms.TextBox();
			this.btnSelectTitle = new System.Windows.Forms.Button();
			this.label6 = new System.Windows.Forms.Label();
			this.textSelectedTitle = new System.Windows.Forms.TextBox();
			this.textSelectedEpisode = new System.Windows.Forms.TextBox();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.label7 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textSelectedShow
			// 
			this.textSelectedShow.Location = new System.Drawing.Point(360, 66);
			this.textSelectedShow.Name = "textSelectedShow";
			this.textSelectedShow.Size = new System.Drawing.Size(288, 20);
			this.textSelectedShow.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(128, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "Guesses for the show";
			// 
			// listShows
			// 
			this.listShows.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2});
			this.listShows.FullRowSelect = true;
			this.listShows.HideSelection = false;
			this.listShows.Location = new System.Drawing.Point(16, 32);
			this.listShows.Name = "listShows";
			this.listShows.Size = new System.Drawing.Size(296, 80);
			this.listShows.TabIndex = 2;
			this.listShows.UseCompatibleStateImageBehavior = false;
			this.listShows.View = System.Windows.Forms.View.Details;
			this.listShows.SelectedIndexChanged += new System.EventHandler(this.listShows_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Guess";
			this.columnHeader1.Width = 200;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Percentage";
			this.columnHeader2.Width = 72;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(357, 47);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "Selected show:";
			// 
			// listEpisodes
			// 
			this.listEpisodes.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader3,
            this.columnHeader4});
			this.listEpisodes.FullRowSelect = true;
			this.listEpisodes.HideSelection = false;
			this.listEpisodes.Location = new System.Drawing.Point(16, 144);
			this.listEpisodes.Name = "listEpisodes";
			this.listEpisodes.Size = new System.Drawing.Size(296, 80);
			this.listEpisodes.TabIndex = 5;
			this.listEpisodes.UseCompatibleStateImageBehavior = false;
			this.listEpisodes.View = System.Windows.Forms.View.Details;
			this.listEpisodes.SelectedIndexChanged += new System.EventHandler(this.listEpisodes_SelectedIndexChanged);
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Guess";
			this.columnHeader3.Width = 200;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Percentage";
			this.columnHeader4.Width = 72;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 128);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(128, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "Guesses for the episode";
			// 
			// listTitles
			// 
			this.listTitles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader5,
            this.columnHeader6});
			this.listTitles.FullRowSelect = true;
			this.listTitles.HideSelection = false;
			this.listTitles.Location = new System.Drawing.Point(16, 256);
			this.listTitles.Name = "listTitles";
			this.listTitles.Size = new System.Drawing.Size(296, 80);
			this.listTitles.TabIndex = 7;
			this.listTitles.UseCompatibleStateImageBehavior = false;
			this.listTitles.View = System.Windows.Forms.View.Details;
			this.listTitles.SelectedIndexChanged += new System.EventHandler(this.listTitles_SelectedIndexChanged);
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "Guess";
			this.columnHeader5.Width = 200;
			// 
			// columnHeader6
			// 
			this.columnHeader6.Text = "Percentage";
			this.columnHeader6.Width = 72;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 240);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(171, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "Guesses for the episode title";
			// 
			// btnSelectShow
			// 
			this.btnSelectShow.Image = ((System.Drawing.Image)(resources.GetObject("btnSelectShow.Image")));
			this.btnSelectShow.Location = new System.Drawing.Point(328, 64);
			this.btnSelectShow.Name = "btnSelectShow";
			this.btnSelectShow.Size = new System.Drawing.Size(24, 24);
			this.btnSelectShow.TabIndex = 8;
			this.btnSelectShow.Click += new System.EventHandler(this.btnSelectShow_Click);
			// 
			// btnSelectEpisode
			// 
			this.btnSelectEpisode.Image = ((System.Drawing.Image)(resources.GetObject("btnSelectEpisode.Image")));
			this.btnSelectEpisode.Location = new System.Drawing.Point(328, 176);
			this.btnSelectEpisode.Name = "btnSelectEpisode";
			this.btnSelectEpisode.Size = new System.Drawing.Size(24, 24);
			this.btnSelectEpisode.TabIndex = 13;
			this.btnSelectEpisode.Click += new System.EventHandler(this.btnSelectEpisode_Click);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(357, 160);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(100, 16);
			this.label5.TabIndex = 12;
			this.label5.Text = "Selected season:";
			// 
			// textSelectedSeason
			// 
			this.textSelectedSeason.Location = new System.Drawing.Point(360, 179);
			this.textSelectedSeason.Name = "textSelectedSeason";
			this.textSelectedSeason.Size = new System.Drawing.Size(100, 20);
			this.textSelectedSeason.TabIndex = 11;
			// 
			// btnSelectTitle
			// 
			this.btnSelectTitle.Image = ((System.Drawing.Image)(resources.GetObject("btnSelectTitle.Image")));
			this.btnSelectTitle.Location = new System.Drawing.Point(328, 288);
			this.btnSelectTitle.Name = "btnSelectTitle";
			this.btnSelectTitle.Size = new System.Drawing.Size(24, 24);
			this.btnSelectTitle.TabIndex = 16;
			this.btnSelectTitle.Click += new System.EventHandler(this.btnSelectTitle_Click);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(357, 271);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(100, 16);
			this.label6.TabIndex = 15;
			this.label6.Text = "Selected title:";
			// 
			// textSelectedTitle
			// 
			this.textSelectedTitle.Location = new System.Drawing.Point(360, 290);
			this.textSelectedTitle.Name = "textSelectedTitle";
			this.textSelectedTitle.Size = new System.Drawing.Size(288, 20);
			this.textSelectedTitle.TabIndex = 14;
			// 
			// textSelectedEpisode
			// 
			this.textSelectedEpisode.Location = new System.Drawing.Point(466, 179);
			this.textSelectedEpisode.Name = "textSelectedEpisode";
			this.textSelectedEpisode.Size = new System.Drawing.Size(182, 20);
			this.textSelectedEpisode.TabIndex = 17;
			// 
			// btnSave
			// 
			this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnSave.Image = global::EpisodeWizard.App.Resources.Save;
			this.btnSave.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnSave.Location = new System.Drawing.Point(536, 360);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(112, 24);
			this.btnSave.TabIndex = 10;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnCancel.Image = global::EpisodeWizard.App.Resources.Cross;
			this.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnCancel.Location = new System.Drawing.Point(416, 360);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(112, 24);
			this.btnCancel.TabIndex = 9;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(463, 160);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(141, 16);
			this.label7.TabIndex = 18;
			this.label7.Text = "Selected episode number:";
			// 
			// FileGuesses
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(666, 399);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.textSelectedEpisode);
			this.Controls.Add(this.btnSelectTitle);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.textSelectedTitle);
			this.Controls.Add(this.btnSelectEpisode);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.textSelectedSeason);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnSelectShow);
			this.Controls.Add(this.listTitles);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.listEpisodes);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.listShows);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textSelectedShow);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FileGuesses";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Wizard\'s guesses";
			this.Load += new System.EventHandler(this.FileGuesses_Load);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion


		/// <summary>
		/// When the form loads
		/// </summary>
		private void FileGuesses_Load(object sender, EventArgs e)
		{
			btnSelectShow.Enabled = (listShows.SelectedItems.Count > 0);
			btnSelectEpisode.Enabled = (listEpisodes.SelectedItems.Count > 0);
			btnSelectTitle.Enabled = (listTitles.SelectedItems.Count > 0);
		}


		/// <summary>
		/// Displays info about the selected episode file
		/// </summary>
		public void SetEpisodeFile(Core.EpisodeFile EF)
		{
			DisplayGuesses(listShows, EF.PossibleShows);
			DisplayGuesses(listEpisodes, EF.PossibleEpisodes);
			DisplayGuesses(listTitles, EF.PossibleTitles);

			// Show
			if (EF.PossibleShows.SelectedValue != null)
			{
				textSelectedShow.Text = EF.PossibleShows.SelectedValue.ShowName;
			}
			else
			{
				textSelectedShow.Text = String.Empty;
			}

			// Episode
			if (EF.PossibleEpisodes.SelectedValue != null)
			{
				textSelectedSeason.Text = EF.PossibleEpisodes.SelectedValue.Season.ToString();
				textSelectedEpisode.Text = EF.PossibleEpisodes.SelectedValue.Episode.ToString();
			}
			else
			{
				textSelectedSeason.Text = String.Empty;
				textSelectedEpisode.Text = String.Empty;
			}

			// Title
			textSelectedTitle.Text = EF.PossibleTitles.SelectedValue ?? String.Empty;
		}


		/// <summary>
		/// Display all the guesses (guess and proba) in a list
		/// </summary>
		private void DisplayGuesses<T>(ListView List, Core.Guess<T> XGuess)
		{
			float WeightSum = 0;
			foreach (KeyValuePair<T, float> P in XGuess.Choices)
			{
				WeightSum += P.Value;
			}

			List.Items.Clear();
			foreach (KeyValuePair<T, float> P in XGuess.Choices)
			{
				ListViewItem Item = new ListViewItem(P.Key.ToString());
				int Percentage = (int)(P.Value * 100 / WeightSum);
				Item.SubItems.Add(Percentage + "%");
				List.Items.Add(Item);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Not implemented yet");
			this.Close();
		}


		/// <summary>
		/// Choose the select show from the list
		/// </summary>
		private void btnSelectShow_Click(object sender, EventArgs e)
		{
			Debug.Assert(listShows.SelectedItems.Count > 0, "Button shouldn't be enabled");
			textSelectedShow.Text = listShows.SelectedItems[0].Text;
		}

		/// <summary>
		/// Choose the select season and episode from the list
		/// </summary>
		private void btnSelectEpisode_Click(object sender, EventArgs e)
		{
			Debug.Assert(listShows.SelectedItems.Count > 0, "Button shouldn't be enabled");
			string SelectedEpisode = listEpisodes.SelectedItems[0].Text;
			int Pos = SelectedEpisode.IndexOf(".");
			textSelectedSeason.Text = SelectedEpisode.Substring(0, Pos);
			textSelectedEpisode.Text = SelectedEpisode.Substring(Pos+1);
		}

		/// <summary>
		/// Choose the select title from the list
		/// </summary>
		private void btnSelectTitle_Click(object sender, EventArgs e)
		{
			Debug.Assert(listShows.SelectedItems.Count > 0, "Button shouldn't be enabled");
			textSelectedShow.Text = listShows.SelectedItems[0].Text;
		}

		private void listShows_SelectedIndexChanged(object sender, EventArgs e)
		{
			btnSelectShow.Enabled = (listShows.SelectedItems.Count > 0);
		}

		private void listEpisodes_SelectedIndexChanged(object sender, EventArgs e)
		{
			btnSelectEpisode.Enabled = (listEpisodes.SelectedItems.Count > 0);
		}

		private void listTitles_SelectedIndexChanged(object sender, EventArgs e)
		{
			btnSelectTitle.Enabled = (listTitles.SelectedItems.Count > 0);
		}


	}
}
